package com.bjfu.mapper;

import com.bjfu.model.Team;
import com.bjfu.model.User;
import com.bjfu.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;

@Mapper
public interface UserMapper {


        // 插入一个新的用户
        @Insert("INSERT INTO user (username, password, role, status) VALUES (#{username}, #{password}, #{role}, #{status})")
        @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
        int insertUser(User user);

        // 更新用户信息
        @Update("UPDATE user SET username = #{username}, password = #{password}, role = #{role}, status = #{status} WHERE user_id = #{userId}")
        void updateUser(User user);

        // 删除用户
        @Delete("DELETE FROM user WHERE user_id = #{userId}")
        int deleteUser(Long userId);

        // 根据用户 ID 获取用户
        @Select("SELECT * FROM user WHERE user_id = #{userId}")
        User getUserById(Long userId);
        /*@Insert("INSERT INTO `user` (`username`, `role`, `password`, `status`) " +
                "VALUES (#{username}, #{role}, #{password}, #{status})")
        @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
        int insertUser(String username, Integer role, String password, Integer status);*/

        // 获取所有用户
        @Select("SELECT * FROM user")
        List<User> getAllUsers();

        // 根据用户名查询用户
        @Select("SELECT * FROM user WHERE username = #{username}")
        User getUserByUsername(String username);

        // 根据用户名查询用户
        // 更新用户权限
        @Update("UPDATE user SET permit = #{permit} WHERE user_id = #{user_id}")
        int updateUserPermissions(@Param("user_id") Long userId, @Param("permit") String permit);

        // 查询所有团队管理员
        @Select("SELECT * FROM user WHERE role = 1")
        List<User> getAllTeamAdmins();  // 返回一个List<User>，包含ID和姓名,团队管理员role为1

        //查询所有的团队成员信息
        @Select("SELECT ui.* FROM user_info ui " +
                "JOIN user u ON ui.user_id = u.user_id " +
                "WHERE u.role = 2")
        List<UserInfo> getAllTeammembers();// 返回一个List<Userinfo>，包含团队成员信息

        // 根据 userId 获取单个团队成员的详细信息
        @Select("SELECT * FROM user_info WHERE user_id = #{userId}")
        UserInfo getMemberById(Long userId);

        // 根据 user_id 获取用户名
        @Select("SELECT username FROM user WHERE user_id = #{user_id}")
        String getUsernameById(Long user_id);

        @Update("UPDATE user SET status = #{pass_status} WHERE user_id = #{user_id}")
        int judgeMember(Long user_id, Integer pass_status);

        @Update("UPDATE user SET password = #{password} WHERE user_id = #{user_id}")
        int updatePassword(Long user_id, String password);
        // 根据 用户名 获取 id
        @Select("SELECT user_id FROM user WHERE username = #{username}")
        Long getMemberByName(String username);

        @Select("SELECT permit FROM user WHERE user_id = #{userId}")
        String getPermitByUserId(Long userId);

        @Update("UPDATE user SET password = #{tempPassword} WHERE user_id = #{userId}")
        boolean updateUserPassword(Long userId, String tempPassword);

        @Update("UPDATE user SET status = #{i} WHERE user_id = #{userId}")
        int updateUserStatus(Long userId, int i);
}